<?php
namespace Common\Model;

class ReadLogModel extends XuecheModel {

    protected $pk = 'read_id';
    protected $tableName = 'read_log';
    protected $fields = array(
            'read_id','book_id','article_id', 'read_time','member_id'
        );


    public function Validate($data) {
        if($data['book_id'] > 0 && $data['article_id'] > 0 && $data['member_id'] > 0){
            return true;
        }
        return false;
    }

    //添加会员的阅读记录
    public function addReadLog(){
        if(!$this->Validate($data)){
            return false;
        }
        $data['read_time'] = time();
        $where = "`member_id` = '{$data['member_id']}' AND `book_id` = '{$data['book_id']}' AND `article_id` = '{$data['article_id']}'";
        if($this->where($where)->find()){
            return $this->where($where)->save($data);
        }else{
            return $this->add($data);
        }
        return false;
    }


    //获取会员的阅读记录
    public function getReadLog($member_id){
        if($member_id > 0){
            $list = $this->where("`member_id` = '{$member_id}'")->limit(20)->select();
            if(is_array($list)){
                $Book = new BookModel();
                $Article = new BookArticleModel();
                foreach ($list as $key => $value) {
                    $list['article_name'] = $Article->getArticleName($value['article_id']);
                    $list['book_name'] = $Book->getBookName($value['book_id']);
                }
            }
        }
    }











}
